package com.ityang.hhhh.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.ityang.hhhh.conm.R;
import com.ityang.hhhh.domain.OrderDetail;
import com.ityang.hhhh.domain.Orders;
import com.ityang.hhhh.service.OrderDetailService;
import com.ityang.hhhh.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 订单
 */
@RestController
@RequestMapping("/order")
public class OrdersController {

    @Autowired
    private OrdersService ordersService;

    @Autowired
    private OrderDetailService orderDetailService;
    /**
     * 添加订单信息
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders){
        ordersService.submit(orders);
        return R.success("下单成功");
    }

    /**
     * 根据用户id查询订单列表
     * @param
     * @param
     * @return
     */
    @GetMapping("/userList")
    public R<List<Orders>> userPage(Orders orders){
        LambdaQueryWrapper<Orders> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Orders::getUserId,orders.getUserId());
        List<Orders> ordersList = ordersService.list(lqw);
        return R.success(ordersList);
    }

    /**
     * 根据订单号id查询订单详细信息
     * @param orders
     * @return
     */
    @GetMapping("/details")
    public R getById(@RequestBody OrderDetail orders){
        OrderDetail serviceById = orderDetailService.getById(orders);
        return R.success(serviceById);
    }


    /**
     * 后台订单分页
     * @param page
     * @param pageSize
     * @param orders
     * @return
     */
    @GetMapping("/page")
    public R<Page> pagr(Integer page,Integer pageSize,Orders orders){
        Page<Orders> pageInfo = new Page<>(page,pageSize);
        LambdaQueryWrapper<Orders> lqw = new LambdaQueryWrapper<>();
        lqw.like(orders.getNumber() != null,Orders::getNumber,orders.getNumber());
        lqw.like(orders.getOrderTime() != null,Orders::getOrderTime,orders.getOrderTime());

        ordersService.page(pageInfo,lqw);
        return R.success(pageInfo);
    }

    /**
     * 派送
     * @return
     */
    @PutMapping
    public R<String> pass(@RequestBody Orders orders){
        LambdaUpdateWrapper<Orders> lqw = new LambdaUpdateWrapper<>();
        //订单状态 1待付款，2待派送，3已派送，4已完成，5已取消   status: 3
        if (orders.getStatus() == 2) {
            lqw.eq(Orders::getNumber, orders.getNumber())
                    .set(Orders::getStatus, 3);
            return R.success("已派送");
        }
        if (orders.getStatus() == 3) {
            lqw.eq(Orders::getNumber, orders.getNumber())
                    .set(Orders::getStatus, 4);
            return R.success("已完成派送");
        }
        if (orders.getStatus() == 4) {
            lqw.eq(Orders::getNumber, orders.getNumber())
                    .set(Orders::getStatus, 5);
            return R.success("已取消");
        }
        return R.error("错误");
    }

}
